package com.bankscene.screenfit.service;

import com.bankscene.screenfit.model.DouBanMovie;
import com.bankscene.screenfit.model.GitHubUser;
import com.bankscene.screenfit.model.HttpResult;
import com.bankscene.screenfit.util.HttpUtils;

import java.util.List;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by zhaojin on 6/28/16.
 */

public class UrlInterface {

    public interface GitHubService {
        @GET("users/{user}")
        Call<GitHubUser> getUserInfoByCall(@Path("user") String user);

        @GET("users/{user}")
        Observable<GitHubUser> getUserInfoByObservable(@Path("user") String user);
    }

    public interface DouBanService {
        @GET("top250")
        Call<DouBanMovie> getTopMovie(@Query("start") int start, @Query("count") int count);

        @GET("top250")
        Observable<DouBanMovie> getTopMovieByObservable(@Query("start") int start, @Query("count") int count);


        @GET("top250")
        Observable<HttpResult<List<DouBanMovie>>> getTopMovieByHttpResult(@Query("start") int start, @Query("count") int count);

        class Ut {

            private static Ut ut;

            private DouBanService movieService;

            private Ut() {
                movieService = HttpUtils.getServiceByRxAndOkHttp(DouBanService.class, "https://api.douban.com/v2/movie/");
            }

            public static Ut newInstance() {
                if (null == ut) {
                    ut = new Ut();
                }
                return ut;
            }

            /**
             * 用于获取豆瓣电影Top250的数据
             *
             * UrlInterface.DouBanService.Ut.newInstance().getTopMovie(new Subscriber<DouBanMovie>() {
             *
             * @Override public void onCompleted() {
             * log.error("onCompleted");
             * }
             * @Override public void onError(Throwable e) {
             * log.error("onError", e);
             * }
             * @Override public void onNext(DouBanMovie douBanMovie) {
             * log.error("onNext {}", douBanMovie);
             * }
             * }, 0, 10);
             *
             * @param subscriber 由调用者传过来的观察者对象
             * @param start      起始位置
             * @param count      获取长度
             */
            public void getTopMovie(Subscriber<DouBanMovie> subscriber, int start, int count) {
                movieService.getTopMovieByObservable(start, count)
                        .subscribeOn(Schedulers.io())
                        .unsubscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(subscriber);
            }


        }

    }

}
